import java.util.ArrayList;

import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.TranslateTransition;
import javafx.geometry.Point2D;
import javafx.scene.layout.FlowPane;
import javafx.util.Duration;

public class Parade extends FlowPane{

	private Game delegator;
	private  ArrayList<Card> cards = new ArrayList<Card>();
	private  ArrayList<Card> removeList = new ArrayList<Card>();
	int removeCount = 0;


	FadeTransition fadeAnimation = new FadeTransition();
	ScaleTransition smaller = new ScaleTransition();
	ParallelTransition parallel = new ParallelTransition(smaller,fadeAnimation);

	public void setDelegator(Game g){
		delegator = g;
	}
	
	public Parade(){
		this.setPrefWidth(800);	
		
		fadeAnimation.setToValue(0);
		fadeAnimation.setDuration(Duration.millis(500));
		smaller.setFromX(1.0);
		smaller.setFromY(1.0);
		smaller.setToX(2.0);
		smaller.setToY(2.0);
		smaller.setDuration(Duration.millis(200));
	}
	

	public void addCard(Card cd){
		cards.add(cd);
		cd.turnUp();
		this.getChildren().add(cd);
		System.out.println("Parade add card:number-"+ cd.number+" color-"+cd.color);
	}
	
	
	//with delegate
	public void newAddCard(Card cd){
		 cd = new Card(cd);
		 cards.add(cd);
		 this.getChildren().add(cd);
		 cd.turnUp();
		 cd.setTranslateX(50);
		 
		 FadeTransition paradefadeAnimation = new FadeTransition();
		 paradefadeAnimation.setFromValue(0);
		 paradefadeAnimation.setToValue(1);
		 paradefadeAnimation.setDuration(Duration.millis(500));
		 paradefadeAnimation.setNode(cd);
		 
		 TranslateTransition putToParadeAnimation = new TranslateTransition();
		 putToParadeAnimation.setByX(-50);
		 putToParadeAnimation.setDuration(Duration.millis(500));
		 putToParadeAnimation.setNode(cd);
		 
		 ParallelTransition parallel = new ParallelTransition(paradefadeAnimation,putToParadeAnimation);
		 parallel.play();
		 parallel.setOnFinished(e->{
			 delegator.paradeAddFinished();
		 });
	}
	
	//一张一张remove动画的话会累加有问题
	public void remove(int index){
		cards.remove(index);
		this.getChildren().remove(index);
	}
	
	public void removeWhatShouldBeRemoved(){
		Card tail = this.getTailCard();
		removeList.clear();
		for(int j=this.length()-tail.number-1;j>0;j--){
			
			 Card temp = this.at(j-1);
			 //符合条件的被parade清理
			 if(temp.color==tail.color || temp.number<=tail.number){
				 removeCount++;
				 removeList.add(temp);
				 
				 FadeTransition fade = new FadeTransition();
				 fade.setNode(temp);
				 fade.setToValue(0);
				 fade.setDuration(Duration.millis(500));
				 fade.play();
				 fade.setOnFinished(e->{
					 removeCount--;
					 if(removeCount == 0 ){//动画全部结束的时候
						 //去除子节点
						 for(int m=0;m<removeList.size();m++){
							 cards.remove(removeList.get(m)); 
							 this.getChildren().remove(removeList.get(m));
						 }
						 delegator.paradeRemoveFinished(removeList);
					 }
				 });
			 }
		 } 
		
		
		//没牌去除的时候
		 if(removeList.size() == 0 ){//动画全部结束的时候
			 delegator.paradeRemoveFinished(removeList);
		 }
	}
	
	public int length(){
		return cards.size();
	}
	public Card at(int index){
		return cards.get(index);
	}
	
	public void clear(){
		this.getChildren().clear();
		cards.clear();
	}
	
	
	public Card getTailCard(){
		
		if(this.length()==0){
			System.out.println("parade:empty parade");
			return null;
		}
		else{
			return cards.get(this.length()-1);
		}
	}
	
}
